from random import randint
from time import sleep
from selenium.webdriver.support import expected_conditions as a
from selenium import webdriver
from HTMLTestRunner import HTMLTestRunner
import unittest

from selenium.webdriver.support.select import Select


class JCX_Test(unittest.TestCase):
    driver = webdriver.Firefox()

    def test_JCX_Login(self):
        self.driver.get('http://localhost/hug/index.asp')
        sleep(1)
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        sleep(1)
        self.assertIn('后台管理系统', self.driver.page_source)

    def test_JCX_Del_U(self):
        sleep(1)
        self.driver.switch_to.frame('left')
        sySet = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1) > img:nth-child(1)'
        self.driver.find_element_by_css_selector(sySet).click()
        sleep(1)
        JS = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(JS).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_name('chkall').click()
        DS = 'input.button:nth-child(2)'
        self.driver.find_element_by_css_selector(DS).click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertNotIn('name="ID"', self.driver.page_source)

    def test_JXC_Add_U(self):
        bt_set = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(bt_set).click()
        self.driver.find_element_by_name('danwei').send_keys('aa')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('aa', self.driver.page_source)

    def test_JXC_Add_D(self):
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('left')
        CS = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(CS).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)
        TS = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(TS).click()
        self.driver.find_element_by_name('bigclass').send_keys('军事')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('军事', self.driver.page_source)

    def test_JXC_Add_X(self):
        self.driver.switch_to.default_content()
        # 添加小类
        self.driver.switch_to.frame('left')
        CX_S = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1) > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2)'
        self.driver.find_element_by_css_selector(CX_S).click()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        sleep(1)
        TX_S = 'input.button:nth-child(1)'
        self.driver.find_element_by_css_selector(TX_S).click()
        Q = '.toptable > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > select:nth-child(1)'
        sel = self.driver.find_element_by_css_selector(Q)
        lst = Select(sel)
        lst_count = len(lst.options)
        rand = randint(0, lst_count - 1)
        lst.select_by_index(rand)
        sleep(1)
        self.driver.find_element_by_name('smallclass').send_keys('汽车')
        sleep(1)
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.assertIn('汽车', self.driver.page_source)

    def test_JXC_SQ(self):
        self.driver.switch_to.default_content()
        # 切换到顶部框架
        self.driver.switch_to.frame('topFrame')
        self.driver.find_element_by_link_text('安全退出').click()
        sleep(1)
        alt = a.alert_is_present()(self.driver)
        if alt:
            alt.accept()
        sleep(3)
        self.driver.switch_to.default_content()
        self.assertIn('系统登陆', self.driver.page_source)

    def test_quit(self):
        self.driver.quit()


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(JCX_Test('test_JCX_Login'))
    suite.addTest(JCX_Test('test_JCX_Del_U'))
    suite.addTest(JCX_Test('test_JXC_Add_U'))
    suite.addTest(JCX_Test('test_JXC_Add_D'))
    suite.addTest(JCX_Test('test_JXC_Add_X'))
    suite.addTest(JCX_Test('test_JXC_SQ'))
    suite.addTest(JCX_Test('test_quit'))
    return suite


with open('d:/JXC_test.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='进存销测试报告')
    runner.run(test_suite())